const app = getApp();
const WXAPI = require("apifm-wxapi");
const AUTH = require("../../utils/auth");

Page({
  /**
   * 页面的初始数据
   */
  data: {
    balance: 0.0,
    freeze: 0,
    score: 0,
    score_sign_continuous: 0,
    cashlogs: undefined,
    listType: 2, // 1为1个商品一行，2为2个商品一行
    name: "", // 搜索关键词
    orderBy: "", // 排序规则
    page: 1, // 读取第几页
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.setData({
    //   name: options.name,
    //   categoryId: options.categoryId
    // })
    this.search();
    this.readConfigVal();
    // 补偿写法
    getApp().configLoadOK = () => {
      this.readConfigVal();
    };
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    AUTH.checkHasLogined().then((isLogined) => {
      if (isLogined) {
        this.doneShow();
      } else {
        wx.showModal({
          title: "提示",
          content: "本次操作需要您的登录授权",
          cancelText: "暂不登录",
          confirmText: "前往登录",
          success(res) {
            if (res.confirm) {
              wx.switchTab({
                url: "/pages/my/index",
              });
            } else {
              wx.navigateBack();
            }
          },
        });
      }
    });
  },
  doneShow: function () {
    const _this = this;
    const token = wx.getStorageSync("token");
    WXAPI.userAmount(token).then(function (res) {
      if (res.code == 0) {
        _this.setData({
          balance: res.data.balance.toFixed(2),
          freeze: res.data.freeze.toFixed(2),
          totleConsumed: res.data.totleConsumed.toFixed(2),
          score: res.data.score,
        });
      } else {
        wx.showToast({
          title: res.msg,
          icon: "none",
        });
      }
    });
    // 读取积分明细
    WXAPI.scoreLogs({
      token: token,
      page: 1,
      pageSize: 50,
    }).then((res) => {
      if (res.code == 0) {
        _this.setData({
          cashlogs: res.data.result,
        });
      }
    });
  },

  recharge: function (e) {
    wx.navigateTo({
      url: "/pages/recharge/index",
    });
  },
  withdraw: function (e) {
    wx.navigateTo({
      url: "/pages/withdraw/index",
    });
  },

  readConfigVal() {
    const show_seller_number = wx.getStorageSync("show_seller_number");
    const goods_search_show_type = wx.getStorageSync("goods_search_show_type");
    let listType = 1;
    if (goods_search_show_type == 2) {
      listType = 2;
    }
    this.setData({
      show_seller_number,
      listType,
    });
  },
  async search() {
    wx.showLoading({
      title: "加载中",
    });
    const _data = {
      orderBy: this.data.orderBy,
      scoreGoods: true,
      page: this.data.page,
      pageSize: 20,
    };
    if (this.data.name) {
      _data.k = this.data.name;
    }
    if (this.data.categoryId) {
      _data.categoryId = this.data.categoryId;
    }
    const res = await WXAPI.goodsv2(_data);
    wx.hideLoading();
    if (res.code == 0) {
      if (this.data.page == 1) {
        this.setData({
          goods: res.data.result,
        });
      } else {
        this.setData({
          goods: this.data.goods.concat(res.data.result),
        });
      }
    } else {
      if (this.data.page == 1) {
        this.setData({
          goods: null,
        });
      } else {
        wx.showToast({
          title: "没有更多了",
          icon: "none",
        });
      }
    }
  },
  onReachBottom() {
    this.setData({
      page: this.data.page + 1,
    });
    this.search();
  },
  changeShowType() {
    if (this.data.listType == 1) {
      this.setData({
        listType: 2,
      });
    } else {
      this.setData({
        listType: 1,
      });
    }
  },
  bindinput(e) {
    this.setData({
      name: e.detail.value,
    });
  },
  bindconfirm(e) {
    this.setData({
      page: 1,
      name: e.detail.value,
    });
    this.search();
  },
  filter(e) {
    this.setData({
      page: 1,
      orderBy: e.currentTarget.dataset.val,
    });
    this.search();
  },

  async addShopCar(e) {
    const curGood = this.data.goods.find((ele) => {
      return ele.id == e.currentTarget.dataset.id;
    });
    if (!curGood) {
      return;
    }
    if (curGood.stores <= 0) {
      wx.showToast({
        title: "已售罄~",
        icon: "none",
      });
      return;
    }
    if (!curGood.propertyIds && !curGood.hasAddition) {
      // 直接调用加入购物车方法
      const res = await WXAPI.shippingCarInfoAddItem(
        wx.getStorageSync("token"),
        curGood.id,
        1,
        []
      );
      if (res.code == 30002) {
        // 需要选择规格尺寸
        this.setData({
          skuCurGoods: curGood,
        });
      } else if (res.code == 0) {
        wx.showToast({
          title: "加入成功",
          icon: "success",
        });
        wx.showTabBar();
        TOOLS.showTabBarBadge(); // 获取购物车数据，显示TabBarBadge
      } else {
        wx.showToast({
          title: res.msg,
          icon: "none",
        });
      }
    } else {
      // 需要选择 SKU 和 可选配件
      this.setData({
        skuCurGoods: curGood,
      });
    }
  },
});
